/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.renderer
{
	import flash.events.MouseEvent;
	
	import multimedia.control.event.media.SaveTextEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.ViewLocator;
	import multimedia.model.vo.FeedItemVO;
	import multimedia.model.vo.FeedVO;
	import multimedia.model.vo.ImageVO;
	import multimedia.model.vo.MediaVO;
	import multimedia.model.vo.TextVO;
	import multimedia.model.vo.UserVO;
	import multimedia.util.HTMLUtil;
	
	import mx.utils.StringUtil;

	public class CreateTextFromFeedItemButtonRenderer extends ImageBorder
	{

		private var _feedItem:FeedItemVO;

		public function CreateTextFromFeedItemButtonRenderer()
		{
			super();
			this.height = 16;
			this.width = 16;
			this.source = "multimedia/assets/document-text-image.png";
			this.toolTip = "Textmedium mit Bild erstellen";
			this.addEventListener(MouseEvent.CLICK, createTextMedia);
		}

		private function createTextMedia(event:MouseEvent):void
		{
			var reg:RegExp = new RegExp("(?<=src=['|\"])[^'|\"]*?(?=['|\"])","gi");
			var result:Array = _feedItem.description.match(reg);
			if(result != null)
			{
				result = _feedItem.description.match(new RegExp("(?<=href=['|\"])[^'|\"]*?(?=['|\"])","gi"));
			}
			
			var user:UserVO = new UserVO();
			user.user_id = 11;
			var media:MediaVO = new MediaVO();
			media.reference = _feedItem.feed_id.toString();
			media.media_id = _feedItem.title + new Date().getTime();
			media.title = _feedItem.title;
			media.addedBy = user;
			media.duration = 20;
			media.approved = true;
			media.valid_date = new Date().getTime()/1000 + 60 * 60 * 7;
			media.newsticker = true;
			media.type = MediaVO.TEXT;
			
			var url:String = result == null ? "" : result[0];

			var text:TextVO = TextVO.getInstance(media, 
												 StringUtil.trim(HTMLUtil.stripHtmlTags(HTMLUtil.removeHref(_feedItem.description.replace(new RegExp("(?<=title=['|\"])[^'|\"]*?(?=['|\"])","gi"), "")))),
												 StringUtil.trim(HTMLUtil.stripHtmlTags(HTMLUtil.removeHref(_feedItem.description.replace(new RegExp("(?<=title=['|\"])[^'|\"]*?(?=['|\"])","gi"), "")), "b, u, i")),
												 ViewLocator.getInstance().feedContentView.feed.background_color,
												 "",
												 ImageVO.getInstance(media,
												 					 result == null ? "" : result[0],
												 					 _feedItem.title,
												 					 true),
												 null,
												 TextVO.LEFT,
			"",0,0,0,0);
			
			var feed:FeedVO = ModelLocator.getInstance().feeds.getItemAt(0) as FeedVO;
			var i:Number = 0;
			while(_feedItem.feed_id != feed.feed_id)
			{
				feed = ModelLocator.getInstance().feeds.getItemAt(++i) as FeedVO;
			}	
			text.image.image_source = feed.title;
			text.background_color = 0xFFFFFF;
			text.image = url == null || url == "" ? null : text.image;
			new SaveTextEvent(text, user).dispatch();											 
		}

		// getter for node data
		[Bindable]
		public function get node():FeedItemVO
		{
			return _feedItem;
		}

		// setter for node data
		public function set node(value:FeedItemVO):void
		{
			this._feedItem = value;
		}

	}
}